package org.example.cet.demos.service.impl;

import org.example.cet.demos.domain.po.ScoreBank;
import org.example.cet.demos.domain.po.Submission;
import org.example.cet.demos.mapper.AnswerMapper;
import org.example.cet.demos.mapper.ScoreBankMapper;
import org.example.cet.demos.mapper.ScoreDetailMapper;
import org.example.cet.demos.mapper.SubmissionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AnswerServiceImpl {
    @Autowired
    private AnswerMapper answerMapper;
    @Autowired
    private SubmissionMapper submissionMapper;
    @Autowired
    private ScoreDetailMapper scoreDetailMapper;
    @Autowired
    private ScoreBankMapper scoreBankMapper;

    public Map<String, Object> calculation(Submission submission, String stuId, Integer qid, String[] answer) {
        Integer qType = answerMapper.getType(qid);
        String[] ans = (answerMapper.getAns(qid)).split("");
        Map<String, Object> map = new HashMap<>();

        BigDecimal s = BigDecimal.ZERO;
        ArrayList<Map<String, Object>> m = new ArrayList<>();
        StringBuilder resultBuilder = new StringBuilder();
        double[] qscore=new double[ans.length];



        if (qType == 1) {
//            double s11 = 0;
//            double s21 = 0;
            BigDecimal s11 = BigDecimal.ZERO;
            BigDecimal s21 = BigDecimal.ZERO;
            for (int i = 0; i < 15; i++) {
                if (answer[i] != null) {
                    if (answer[i].equals(ans[i])) {
//                        s11 += 7.1;
                        s11 = s11.add(new BigDecimal("7.1"));
                        Map<String, Object> m1 = new HashMap<>();
                        resultBuilder.append("1");
                        qscore[i]=7.1;
                        m1.put("题号", i + 1);
                        m1.put("答题情况", "正确");
                        m1.put("您的答案", answer[i]);
                        m1.put("正确答案", ans[i]);
                        m1.put("您的得分",7.1);
                        m.add(m1);
                    } else {
                        Map<String, Object> m1 = new HashMap<>();
                        resultBuilder.append("2");
                        qscore[i]= (double) 0;
                        m1.put("题号", i + 1);
                        m1.put("答题情况", "错误");
                        m1.put("您的答案", answer[i]);
                        m1.put("正确答案", ans[i]);
                        m1.put("您的得分",0);
                        m.add(m1);
                    }
                } else {
                    Map<String, Object> m1 = new HashMap<>();
                    resultBuilder.append("0");
                    qscore[i]= (double) 0;
                    m1.put("题号", i + 1);
                    m1.put("答题情况", "未答");
                    m1.put("您的答案", answer[i]);
                    m1.put("正确答案", ans[i]);
                    m1.put("您的得分",0);
                    m.add(m1);
                }
            }
            for (int i = 15; i < 25; i++) {
                if (answer[i] != null) {
                    if (answer[i].equals(ans[i])) {
//                        s21 += 14.2;
                        s21 = s21.add(new BigDecimal("14.2"));
                        Map<String, Object> m2 = new HashMap<>();
                        resultBuilder.append("1");
                        qscore[i]=14.2;
                        m2.put("题号", i + 1);
                        m2.put("答题情况", "正确");
                        m2.put("您的答案", answer[i]);
                        m2.put("正确答案", ans[i]);
                        m2.put("您的得分",14.2);
                        m.add(m2);
                    } else {
                        Map<String, Object> m2 = new HashMap<>();
                        resultBuilder.append("2");
                        qscore[i]= (double) 0;
                        m2.put("题号", i + 1);
                        m2.put("答题情况", "错误");
                        m2.put("您的答案", answer[i]);
                        m2.put("正确答案", ans[i]);
                        m2.put("您的得分",0);
                        m.add(m2);
                    }
                } else {
                    Map<String, Object> m2 = new HashMap<>();
                    resultBuilder.append("0");
                    qscore[i]= (double) 0;
                    m2.put("题号", i + 1);
                    m2.put("答题情况", "未答");
                    m2.put("您的答案", answer[i]);
                    m2.put("正确答案", ans[i]);
                    m2.put("您的得分",0);
                    m.add(m2);
                }
            }
            s = s11 .add( s21 );
        }
        if (qType == 2) {
            for (int i = 0; i < ans.length; i++) {
                if (answer[i] != null) {
                    if (answer[i].equals(ans[i])) {
//                        s += 3.55;
                        s=s.add(new BigDecimal("3.55"));
                        Map<String, Object> m1 = new HashMap<>();
                        resultBuilder.append("1");
                        qscore[i]=3.55;
                        m1.put("题号", i + 1);
                        m1.put("答题情况", "正确");
                        m1.put("您的答案", answer[i]);
                        m1.put("正确答案", ans[i]);
                        m1.put("您的得分",3.55);
                        m.add(m1);
                    } else {
                        Map<String, Object> m1 = new HashMap<>();
                        resultBuilder.append("2");
                        qscore[i]= (double) 0;
                        m1.put("题号", i + 1);
                        m1.put("答题情况", "错误");
                        m1.put("您的答案", answer[i]);
                        m1.put("正确答案", ans[i]);
                        m1.put("您的得分",0);
                        m.add(m1);
                    }
                } else {
                    Map<String, Object> m1 = new HashMap<>();
                    resultBuilder.append("0");
                    qscore[i]= (double) 0;
                    m1.put("题号", i + 1);
                    m1.put("答题情况", "未答");
                    m1.put("您的答案", answer[i]);
                    m1.put("正确答案", ans[i]);
                    m1.put("您的得分",0);
                    m.add(m1);
                }
            }
        }
        if (qType == 3) {
            for (int i = 0; i < ans.length; i++) {
                if (answer[i] != null) {
                    if (answer[i].equals(ans[i])) {
//                        s += 7.1;
                        s=s.add(new BigDecimal("7.1"));
                        Map<String, Object> m1 = new HashMap<>();
                        resultBuilder.append("1");
                        qscore[i]=7.1;
                        m1.put("题号", i + 1);
                        m1.put("答题情况", "正确");
                        m1.put("您的答案", answer[i]);
                        m1.put("正确答案", ans[i]);
                        m1.put("您的得分",7.1);
                        m.add(m1);
                    } else {
                        Map<String, Object> m1 = new HashMap<>();
                        resultBuilder.append("2");
                        qscore[i]= (double) 0;
                        m1.put("题号", i + 1);
                        m1.put("答题情况", "错误");
                        m1.put("您的答案", answer[i]);
                        m1.put("正确答案", ans[i]);
                        m1.put("您的得分",0);
                        m.add(m1);
                    }
                } else {
                    Map<String, Object> m1 = new HashMap<>();
                    resultBuilder.append("0");
                    qscore[i]= (double) 0;
                    m1.put("题号", i + 1);
                    m1.put("答题情况", "未答");
                    m1.put("您的答案", answer[i]);
                    m1.put("正确答案", ans[i]);
                    m1.put("您的得分",0);
                    m.add(m1);
                }
            }
        }
        if (qType == 4) {
            for (int i = 0; i < ans.length; i++) {
                if (answer[i] != null) {
                    if (answer[i].equals(ans[i])) {
//                        s += 14.2;
                        s=s.add(new BigDecimal("14.2"));
                        Map<String, Object> m1 = new HashMap<>();
                        resultBuilder.append("1");
                        qscore[i]=14.2;
                        m1.put("题号", i + 1);
                        m1.put("答题情况", "正确");
                        m1.put("您的答案", answer[i]);
                        m1.put("正确答案", ans[i]);
                        m1.put("您的得分",14.2);
                        m.add(m1);
                    } else {
                        Map<String, Object> m1 = new HashMap<>();
                        resultBuilder.append("2");
                        qscore[i]= (double) 0;
                        m1.put("题号", i + 1);
                        m1.put("答题情况", "错误");
                        m1.put("您的答案", answer[i]);
                        m1.put("正确答案", ans[i]);
                        m1.put("您的得分",0);
                        m.add(m1);
                    }
                } else {
                    Map<String, Object> m1 = new HashMap<>();
                    resultBuilder.append("0");
                    qscore[i]= (double) 0;
                    m1.put("题号", i + 1);
                    m1.put("答题情况", "未答");
                    m1.put("您的答案", answer[i]);
                    m1.put("正确答案", ans[i]);
                    m1.put("您的得分",0);
                    m.add(m1);
                }
            }
        }


        String[] result = null;
        if (resultBuilder != null) {
            result = resultBuilder.toString().split("");
        }

        submission.setAnswer(answer);
        submission.setResult(result);
        submission.setQscore(qscore);
//        submission.setType(type);

        map.put("score",  s.doubleValue());
        map.put("record", m);
        map.put("result", result);
        return map;
    }

    public void createScoreBankRecord(String bankId, String stuId, int type) {
        scoreBankMapper.insert(bankId, stuId, type, 0); // 初始总分设为 0
    }

    public void createScoreDetailRecord(int questionId, int qType, double scorePart, String bankId) {
        scoreDetailMapper.insert(questionId, qType, scorePart, bankId);
    }

    public void updateTotalScore(String bankId) {
        List<Double> specialScores = scoreDetailMapper.getScorePart(bankId);
        double totalScore = 0;
        for (double score : specialScores) {
            totalScore += score;
        }
        scoreBankMapper.updateTotalScore(bankId, totalScore);
    }

    public Map<String, Object> handleSubmission(String title,String bankId, String stuId, Integer qid, String[] answer,Long homeworkId,Integer type) {
        Submission submission = new Submission();
        submission.setTitle(title);
        submission.setStuId(stuId);
        submission.setQuestionId(qid);
        submission.setBankId(bankId);
        submission.setHomeworkId(homeworkId);
        submission.setType(type);
        Map<String, Object> resultMap = calculation(submission, stuId, qid, answer);
        double scorePart = (double) resultMap.get("score");
//        int type = submission.getType();

        // 检查 score_bank 记录是否已存在，如果不存在则创建
        List<ScoreBank> scoreBanks = scoreBankMapper.getPage(bankId);
        if (scoreBanks.isEmpty()) {
            createScoreBankRecord(bankId, stuId, type);
        }
        int qType = answerMapper.getType(qid);
        // 创建 score_detail 记录
        createScoreDetailRecord(qid, qType, scorePart, bankId);

        // 更新 score_bank 总分
        updateTotalScore(bankId);

        // 插入 submission 记录
        submissionMapper.insert(submission);

        return resultMap; // 返回计算结果
    }

    public Map<String, Object> watscore(String title,String bankId, String stuId, Integer qid, String[] answer, Long homeworkId, Integer type, BigDecimal score) {
        Submission submission = new Submission();
        submission.setTitle(title);
        submission.setStuId(stuId);
        submission.setQuestionId(qid);
        submission.setBankId(bankId);
        submission.setHomeworkId(homeworkId);
        submission.setType(type);

        Map<String, Object> map = new HashMap<>();
        BigDecimal s = BigDecimal.ZERO;
        ArrayList<Map<String, Object>> m = null;
        String[] result = null;

        if (type == 0 || type == 5) {
            s = score;
            m = null;
            result = null;
        }

        submission.setAnswer(answer);
        submission.setResult(result);
        submission.setType(type);

        map.put("score", s.doubleValue());
        map.put("record", m);
        map.put("result", result);

        // 检查 score_bank 记录是否已存在，如果不存在则创建
        List<ScoreBank> scoreBanks = scoreBankMapper.getPage(bankId);
        if (scoreBanks.isEmpty()) {
            createScoreBankRecord(bankId, stuId, type);
        }
        int qType = answerMapper.getType(qid);
        // 创建 score_detail 记录
        createScoreDetailRecord(qid, qType, s.doubleValue(), bankId);

        // 更新 score_bank 总分
        updateTotalScore(bankId);

        // 插入 submission 记录
        submissionMapper.insert(submission);

        return map;}

}
